package com.viewplug.listView;


import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;

/**
 * Created by mac_wk on 14-11-10.
 */
public class SwapListViewItem extends LinearLayout{
    private Scroller scroller = null;
    private int leftViewWdith=0;
    public static final int right_state=0;//右侧未滑动状态
    public static final int right_left_state=1;//右侧滑到左侧状态
    public static final int left_state=2;//左侧未滑动状态
    public static final int left_right_state=3;//左侧滑动到右侧的状态
    private   int nowState=0;
    private boolean hashLeft;
    private boolean hashRight;

    public boolean isHashRight() {
        return hashRight;
    }

    public void setHashRight(boolean hashRight) {
        this.hashRight = hashRight;
    }

    public boolean isHashLeft() {
        return hashLeft;
    }

    public void setHashLeft(boolean hashLeft) {
        this.hashLeft = hashLeft;
    }

    public int getNowState() {
        return nowState;
    }

    public void setNowState(int nowState) {
        this.nowState = nowState;
    }

    private void init(Context context)
    {
        nowState = right_state;
        scroller =  new Scroller(context,new DecelerateInterpolator());
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {

                int count = getChildCount();
                if (count==1)//左右不能够滑动
                { leftViewWdith = 0;}
                else if (count==2){
                    int width = getChildAt(1).getWidth();
                    if (width>=getWidth())//如果中间的是大于整体宽度的那么就是左中型
                    {
                        leftViewWdith = getChildAt(0).getWidth();
                        hashLeft = true;
                    }else//否则就是右中型
                    {
                        leftViewWdith = getChildAt(1).getWidth();
                        hashRight = true;
                    }
                }else if (count==3)//左右都能够滑动
                {
                    leftViewWdith = getChildAt(1).getWidth();
                    hashLeft = true;
                    hashRight = true;
                }
                getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

    public SwapListViewItem(Context context) {
        super(context);
        init(context);
    }

    public SwapListViewItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public SwapListViewItem(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.L)
    public SwapListViewItem(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }
    public int getMaxWdith(){


        return this.leftViewWdith;
    }
    @Override
    public void computeScroll() {
        Log.e("message","computeScrollOffset");
        if (scroller.computeScrollOffset())
        {
            Log.e("message","complete scroll");
            scrollBy(scroller.getCurrX(), 0);
            invalidate();
        }
    }

    private int now_x;
    public void rightScroll(int x)
    {
        now_x = x;
        Log.e("message",x+"-");
        this.scrollTo(-x,getScrollY());
    }

    public void scroller() {
        Log.e("message","scroller "+getMaxWdith()+"###"+now_x);
        if(Math.abs(now_x)>(getMaxWdith()/3))
        {
            Log.e("message","startScroll ");
            int s_scroll = getScrollX();
            if (s_scroll>getMaxWdith())
                return;

            scroller.startScroll(0,0,getMaxWdith(),0);

        }
        else
            scroller.startScroll(0,0,0,0);
    }
}
